import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wangxingsheng on 2019/2/12.
 */
public class GsonTest {

    public static void main(String[] args) {
        Gson gson = new Gson();
        test4(gson);
    }

    private static void test4(Gson gson) {
        C<B> c1 = new C<>();
        c1.setData(new B("b1", "12"));
        c1.setCname("BBBB");
        String resultC1 = gson.toJson(c1);
        System.out.println(resultC1);

        C<B> c2 = gson.fromJson(resultC1, new TypeToken<C<B>>() {
        }.getType());
        System.out.println(c2.data.name);
    }

    private static void test3(Gson gson) {
        List<B> list = new ArrayList<>();
        list.add(new B("b1", "12"));
        list.add(new B("b2", "13"));
        list.add(new B("b3", "14"));
        JsonArray itemArray = new Gson().toJsonTree(list, new TypeToken<List<B>>() {
        }.getType()).getAsJsonArray();
        System.out.println(itemArray);
        String targetJson = gson.toJson(list);
        System.out.println(targetJson);
        List<B> target = gson.fromJson(targetJson, new TypeToken<List<B>>() {
        }.getType());
        System.out.println(target.get(1).name);
    }

    private static void test2(Gson gson) {
        List<B> list = new ArrayList<>();
        list.add(new B("b1", "12"));
        list.add(new B("b2", "13"));
        list.add(new B("b3", "14"));
        A a = new A("a1", "10");
        a.setList(list);
        System.out.println(gson.toJson(a));

        JsonArray itemArray = new Gson().toJsonTree(list, new TypeToken<List<B>>() {
        }.getType()).getAsJsonArray();
        JsonObject params = new JsonObject();
        params.add("items", itemArray);
        System.out.println(params);
    }

    private static void test1(Gson gson) {
        List<Map<String, String>> testList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Map<String, String> map = new HashMap<>();
            map.put(i + "", "val " + i);
            testList.add(map);
        }
//        JsonArray jsonArray = gson.fromJson(gson.toJson(testList), JsonArray.class);
        JsonArray jsonArray = gson.fromJson(gson.toJson(testList), new TypeToken<JsonArray>() {
        }.getType());
        jsonArray.remove(3);
        System.out.println(jsonArray);
    }

    static class C<T> {
        T data;
        String cname;

        public T getData() {
            return data;
        }

        public void setData(T data) {
            this.data = data;
        }

        public String getCname() {
            return cname;
        }

        public void setCname(String cname) {
            this.cname = cname;
        }
    }

    static class A {
        List<B> list;
        String name;
        String age;

        public A(String name, String age) {
            this.name = name;
            this.age = age;
        }

        public A() {
        }

        public List<B> getList() {
            return list;
        }

        public void setList(List<B> list) {
            this.list = list;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getAge() {
            return age;
        }

        public void setAge(String age) {
            this.age = age;
        }
    }

    static class B {
        String name;
        String age;

        public B(String name, String age) {
            this.name = name;
            this.age = age;
        }

        public B() {
        }
    }
}
